package com.taijia.chapter6;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Word counter : count english word upper case, lower case, none english word
 * 
 * @author taijia
 * @since 2014年11月21日
 * 
 */
public class WordCounter {

	/**
	 * common function count word
	 * @param word target word
	 * @param regex regex
	 * @return
	 */
	private static int count(String word, String regex) {
		if (word == null || "".equals(word))
			return 0;
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = null;
		int count = 0;
		for (int i = 0; i < word.length(); i++) {
			matcher = pattern.matcher(String.valueOf(word.charAt(i)));
			if (matcher.matches()) {
				count++;
			}
		}
		return count;
	}
	
	/**
	 * count word upper case
	 * @param word target word
	 * @return
	 */
	public static int countUpper(String word) {
		return count(word, "[A-Z]");
	}
	
	/**
	 * count word lower case
	 * @param word target word
	 * @return
	 */
	public static int countLower(String word) {
		return count(word, "[a-z]");
	}
	
	/**
	 * count not word
	 * @param word target word
	 * @return
	 */
	public static int countNotWord(String word) {
		return count(word, "[^a-z|A-Z]");
	}
	
	/**
	 * count sourceWord in targetWord times
	 * @param sourceWord
	 * @param targetWord
	 * @return
	 */
	public static int countTimes(String sourceWord,String targetWord) {
		if (sourceWord == null || "".equals(sourceWord) || targetWord == null || "".equals(targetWord))
			return 0;
		int count = -1;
		for (int index = 0; index > -1; index = targetWord.indexOf(sourceWord, index + 1)) {
			// 第一次走 int index = 0;index>-1;并没有判断到sourceWord 在targetWord
			count++;
		}
		return count;
	}
	
	/**
	 * count sourceWord in targetWord times base on function String.split()
	 * @param sourceWord
	 * @param targetWord
	 * @return
	 */
	public static int countTimesSplit(String sourceWord,String targetWord) {
		if (sourceWord == null || "".equals(sourceWord) || targetWord == null || "".equals(targetWord))
			return 0;
		String[] strs = targetWord.split(sourceWord);
		// 如果 开头结尾都有
		if((targetWord.length() == (targetWord.lastIndexOf(sourceWord) + sourceWord
				.length())) && targetWord.indexOf(sourceWord) == 0) {
			return strs.length;
		} 
		// 如果在最后一个或者第一个
		else if ((targetWord.length() == (targetWord.lastIndexOf(sourceWord) + sourceWord
				.length())) || targetWord.indexOf(sourceWord) == 0 ) {
			if(!"".equals(strs[0])) {
				return strs.length;
			} else {
				// 因为 当第一个出现的时候 会出现第一个第一个元素为空，最后一个出现的时候 不会出现最后一个元素为空
				return strs.length-1;
			}
		} else { 
		// 如果 开头结尾都没有
			return strs.length-1;
		}
	}
	
	public static void main(String[] args) {
		String str = "pppCCC333ddR33370333";
		String strL = "333";
		System.out.println(countUpper(str)); // count upper case
		System.out.println(countLower(str)); // count lower case
		System.out.println(countNotWord(str)); // count not word
		System.out.println(countTimes(strL, str)); // count times
		System.out.println(countTimesSplit(strL, str)); // count times
	}
}
